<?php
// +----------------------------------------------------------------------
// | Name 风音框架
// +----------------------------------------------------------------------
// | Author 唐启云 <tqy@fxri.net>
// +----------------------------------------------------------------------
// | Copyright Copyright © 2016-2099 方弦研究所. All rights reserved.
// +----------------------------------------------------------------------
// | Link https://www.fxri.net
// +----------------------------------------------------------------------
namespace fxapp\facade_ext;

/**
 * 文件类
 */
class File
{
    /**
     * 上传附件
     * @param string $file_name 文件名称
     * @return mixed
     */
    public function uploadAttach($file_name = null)
    {
        // 初始化变量
        $echo = \fxapp\Server::echo();
        $param['file_name'] = $file_name;
        $record = \fxapp\Base::oracle('file')->uploadAttach($param);
        if ($record[0]) {
            $echo[2] = $record[3][0]['attach_url'];
            return $echo;
        } else if ($record[1] == 1000) {
            $echo[2] = '';
            return $echo;
        } else {
            return $record;
        }
    }

    /**
     * 上传视频
     * @param string $file_name 文件名称
     * @return mixed
     */
    public function uploadVideo($file_name = null)
    {
        // 初始化变量
        $echo = \fxapp\Server::echo();
        $param['file_name'] = $file_name;
        $record = \fxapp\Base::oracle('file')->uploadVideo($param);
        if ($record[0]) {
            $echo[2] = $record[3][0]['attach_url'];
            return $echo;
        } else if ($record[1] == 1000) {
            $echo[2] = '';
            return $echo;
        } else {
            return $record;
        }
    }

    /**
     * 上传图片
     * @param string $file_name 文件名称
     * @return mixed
     */
    public function uploadImage($file_name = null)
    {
        // 初始化变量
        $echo = \fxapp\Server::echo();
        $param['file_name'] = $file_name;
        $record = \fxapp\Base::oracle('file')->uploadPicture($param);
        if ($record[0]) {
            $echo[2] = $record[3][0]['attach_url'];
            return $echo;
        } else if ($record[1] == 1000) {
            $echo[2] = '';
            return $echo;
        } else {
            return $record;
        }
    }

    /**
     * 获取最后N行
     * @param string $filename 文件路径
     * @param int $n 最后几行
     * @return mixed
     */
    public function lastLines($filename, $n)
    {
        // false表示有错误，成功则返回字符串
        if (!$fp = fopen($filename, 'r')) {
            echo "打开文件失败，请检查文件路径是否正确，路径和文件名不要包含中文";
            return false;
        }
        $pos = -2;
        $eof = "";
        $str = "";
        while ($n > 0) {
            while ($eof != "\n") {
                if (!fseek($fp, $pos, SEEK_END)) {
                    $eof = fgetc($fp);
                    $pos--;
                } else {
                    break;
                }
            }
            $str = fgets($fp) . $str;
            $eof = "";
            $n--;
        }
        return $str;
    }
}
